package org.kev.transit.util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.kev.transit.data.Direction;
import org.kev.transit.data.Route;
import org.kev.transit.data.RouteType;
import org.kev.transit.data.Stop;
import org.kev.transit.data.TimeTable;

/**
 * maps JSON encoded text into (PJO) data objects.
 * @author kevin
 *
 */
public class JsonHelper {

	/**
	 * Takes json text and converts it to a data object. Note this class make no
     * android calls so it can be run in plain Java.
	 * @param jsonText
	 * @return stop data object
	 * @throws JSONException
	 */
	public static Stop getStop(String jsonText) throws JSONException{
		JSONObject jo = new JSONObject( jsonText);
		System.out.println("jsobj="+jo.toString() );
		Stop stop = new Stop();
		
		stop.id=jo.getString(Stop._ID);
		stop.title=jo.getString(Stop.TITLE);
		stop.lat=jo.getString(Stop.LAT);
		stop.lon=jo.getString(Stop.LON);
		stop.routeTypeId=jo.getString(Stop.ROUTE_TYPE);
		stop.routeId=jo.getString(Stop.ROUTE);
		stop.directionId=jo.getString(Stop.DIRECTION);
		return stop;
		
	}
	/**
	 * Takes json text and converts it to a list of data object using a Json array.
	 * @param jsonText
	 * @return list of Stop objects
	 * @throws JSONException
	 */
	public static ArrayList <Stop> getStops(String jsonText) throws JSONException{
		ArrayList <Stop> list = new ArrayList <Stop>();
		JSONArray ja= new JSONArray(jsonText);
		for (int i=0;i<ja.length();i++){
			String test = ja.getString(i);
			//System.out.println("test="+test);
			list.add(getStop(test) );
		}
		return list;
		
	}	
	/**
	 * Takes json text and converts it to a data object.
	 * @param jsonText
	 * @return route data object
	 * @throws JSONException
	 */
	public static Route getRoute(String jsonText) throws JSONException{
		JSONObject jo = new JSONObject( jsonText);
		System.out.println("jsobj="+jo.toString() );
		Route route = new Route();
		route.id=jo.getString(Route.ROUTE_ID);
		route.routeType=jo.getString(Route.ROUTE_TYPE);
		route.title=jo.getString(Route.TITLE);
		return route;		
	}
	/**
	 * Takes json text and converts it to a list of data object using a Json array.
	 * @param jsonText
	 * @return list of Route objects
	 * @throws JSONException
	 */
		public static ArrayList <Route> getRoutes(String jsonText) throws JSONException{
			ArrayList <Route> list = new ArrayList <Route>();
			JSONArray ja= new JSONArray(jsonText);
			for (int i=0;i<ja.length();i++){
				String test = ja.getString(i);
				
				list.add(getRoute(test) );
			}
			return list;
			
		}
		
	/**
	 * Takes json text and converts it to a data object.
	 * @param jsonText
	 * @return timetable object 
	 * @throws JSONException
	 */
	public static TimeTable getTimeTable(String jsonText) throws JSONException{
		JSONObject jo = new JSONObject( jsonText);
		System.out.println("jsobj="+jo.toString() );
		TimeTable timeTable = new TimeTable();
		/*Stop stop = new Stop();
		stop.id=jo.getString(TimeTable.STOP_ID);
		stop.title=jo.getString(Stop.TITLE);
		stop.lat=jo.getString(Stop.LAT);
		stop.lat=jo.getString(Stop.LON);*/
//		timeTable.createDate=jo.getLong(TimeTable.CREATE_DATE);
		timeTable.id=jo.getString(TimeTable.TIME_ID);
		timeTable.route=jo.getString(TimeTable.ROUTE);
		timeTable.routeType=jo.getString(TimeTable.ROUTE_TYPE);
		timeTable.direction=jo.getString(TimeTable.DIRECTION);
		timeTable.stopId=jo.getString(TimeTable.STOP_ID);
		timeTable.time=jo.getString(TimeTable.TIME);
		return timeTable;
		
	}
/**
 * Takes json text and converts it to a list of data object using a Json array.
 * @param jsonText
 * @return list of TimeTable objects
 * @throws JSONException
 */
	public static ArrayList <TimeTable> getTimeTables(String jsonText) throws JSONException{
		ArrayList <TimeTable> list = new ArrayList <TimeTable>();
		JSONArray ja= new JSONArray(jsonText);
		for (int i=0;i<ja.length();i++){
			String test = ja.getString(i);
			
			list.add(getTimeTable(test) );
		}
		return list;
		
	}
	/**
	 * Takes json text and converts it to a data object.
	 * @param jsonText
	 * @return timetable object 
	 * @throws JSONException
	 */
	public static Direction getDirection(String jsonText) throws JSONException{
		JSONObject jo = new JSONObject( jsonText);
		System.out.println("jsobj="+jo.toString() );
		Direction data = new Direction();
		
		data.title=jo.getString(Direction.TITLE);
		data.id=jo.getString(Direction.DIRECTION_ID);
		data.routeType=jo.getString(Direction.ROUTE_TYPE);
		data.route=jo.getString(Direction.ROUTE);
		return data;
		
	}
/**
 * Takes json text and converts it to a list of data object using a Json array.
 * @param jsonText
 * @return list of TimeTable objects
 * @throws JSONException
 */
	public static ArrayList <Direction> getDirections(String jsonText) throws JSONException{
		System.out.println(jsonText);
		ArrayList <Direction> list = new ArrayList <Direction>();
		
		JSONArray ja= new JSONArray(jsonText);
		for (int i=0;i<ja.length();i++){
			String test = ja.getString(i);
			
			list.add(getDirection(test) );
		}
		return list;
		
	}

	/**
	 * Takes json text and converts it to a data object.
	 * @param jsonText
	 * @return route type data object
	 * @throws JSONException 
	 */
	public static RouteType getRouteType(String jsonText) throws JSONException {
		JSONObject jo = new JSONObject( jsonText);
		System.out.println("jsobj="+jo.toString() );
		RouteType data = new RouteType();
		
		data.title=jo.getString(RouteType.TITLE);
		data.id=jo.getString(RouteType.ROUTE_TYPE_ID);
		return data;
	}
	/**
	 * Takes json text and converts it to a list of data object using a Json array.
	 * @param jsonText
	 * @return list of RouteType objects
	 * @throws JSONException 
	 */
	public static ArrayList<RouteType> getRouteTypes(String jsonText) throws JSONException {
		ArrayList <RouteType> list = new ArrayList <RouteType>();
		JSONArray ja= new JSONArray(jsonText);
		for (int i=0;i<ja.length();i++){
			String test = ja.getString(i);
			
			list.add(getRouteType(test) );
		}
		return list;
	
	}
}
